package com.mytaobao.main.firsttab.func.guessyoulike;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baseproject.adapter.ItemViewDelegate;
import com.baseproject.adapter.ViewHolder;
import com.baseproject.imageload.ImageLoaderUtil;
import com.baseproject.util.UIUtils;
import com.mytaobao.R;
import com.mytaobao.util.Const;

import java.util.ArrayList;
import java.util.List;

/**
 * 猜你喜欢列表
 */
public class GuessYouLikeItemDelegate implements ItemViewDelegate<List<ItemGuessYouLike>> {

    private final String TAG = "GuessYouLikeDelegate";
    private final Fragment mFirstTabFragment;

    private List<ItemGuessYouLike> mDataGuessYouLike = new ArrayList<ItemGuessYouLike>();

    public GuessYouLikeItemDelegate(Fragment firstTabFragment) {
        mFirstTabFragment = firstTabFragment;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.view_guessyoulike;
    }

    @Override
    public int getItemViewType() {
        return Const.TYPE_GUESS_YOU_LIKE;
    }

    @Override
    public void convert(ViewHolder holder, int position) {
        int realPosition = getRealPosition(position);
        GuessYouLikeViewHolder currentHolder = (GuessYouLikeViewHolder)holder;
        ItemGuessYouLike itemGuessYouLike = mDataGuessYouLike.get(realPosition);
        if(holder.getLayoutPosition() % 2 == 0) {
            currentHolder.llLikeItem.setPadding(0,0, UIUtils.dip2px(3),0);
        }

        ImageLoaderUtil.getInstance().with(mFirstTabFragment).load(itemGuessYouLike.getCoverUrl()).into(currentHolder.ivBookCover);
        currentHolder.tvTitle.setText(itemGuessYouLike.getTitle());
        currentHolder.tvLikeItemMoney.setText("￥" + itemGuessYouLike.getPrice());
        currentHolder.tvPayedCount.setText(itemGuessYouLike.getPayedCount() + "人已付款");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return false;
    }

    @Override
    public ViewHolder createViewHolder(Context context, ViewGroup parent) {
        View itemView = LayoutInflater.from(context).inflate(getItemViewLayoutId(), parent, false);
        ViewHolder holder = new GuessYouLikeViewHolder(context, itemView);
        return holder;
    }

    private int getRealPosition(int position) {
        if (position < Const.TYPE_GUESS_YOU_LIKE) {
            return position;
        } else {
            return position - Const.TYPE_GUESS_YOU_LIKE;
        }
    }

    @Override
    public void updateData(List<ItemGuessYouLike> itemGuessYouLikes) {
        this.mDataGuessYouLike = itemGuessYouLikes;
        Log.d(TAG, "[updateData] mDataGuessYouLike = " + mDataGuessYouLike);
//        notifyDataSetChanged();
    }

    private final class GuessYouLikeViewHolder extends ViewHolder {
        private LinearLayout llLikeItem;
        private ImageView ivBookCover;
        private TextView tvTitle;
        private TextView tvLikeItemMoney;
        private TextView tvPayedCount;

        public GuessYouLikeViewHolder(Context context, View itemView) {
            super(context, itemView);
            llLikeItem = (LinearLayout)itemView.findViewById(R.id.llLikeItem);
            ivBookCover = (ImageView) itemView.findViewById(R.id.iv_cover);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvLikeItemMoney = (TextView) itemView.findViewById(R.id.tvLikeItemMoney);
            tvPayedCount = (TextView) itemView.findViewById(R.id.tvPayedCount);
        }
    }

    @Override
    public int getItemCount() {
        return mDataGuessYouLike.size();
    }

    /**
     * 特殊接口，获取元素个数
     * @return
     */
    public int getSize() {
        return mDataGuessYouLike.size();
    }
}
